package ui;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * A writer that wraps multiple {@link Writer} objects and writes to all of them 
 * when any of its <code>write</code> methods are invoked.
 * @author Charles
 *
 */
public class ForkedWriter extends Writer {
	List<Writer> out;
	
	public ForkedWriter(List<? extends Writer> out) {
		this.out = new ArrayList<Writer>(out);
	}
	
	public ForkedWriter(Writer...out) {
		this.out = new ArrayList<Writer>(out.length);
		Collections.addAll(this.out, out);
	}

	@Override
	public void close() throws IOException {
		flush();
	}

	@Override
	public void flush() throws IOException {
		for (Writer w: out) {
			w.flush();
		}
	}

	@Override
	public void write(char[] arg0, int arg1, int arg2) throws IOException {
		for (Writer w : out) {
			w.write(arg0, arg1, arg2);
		}
	}
}
